﻿(function ($) {

    $(function () {
        var scriptbase = _spPageContextInfo.webServerRelativeUrl + "/_layouts/15/";
        $.getScript(scriptbase + "SP.js", function () {
            $.getScript(scriptbase + "SP.Taxonomy.js", GetNews('.category #newlastestslide .carousel-inner', '.category #othernews'));
        });
    });

    this.activeIndicator = function () {

        $('#newlastestslide').on('slid.bs.carousel', function () {
            $holder = $("#newlastestslide ol li.active");
            $holder.removeClass('active');
            var idx = $('div.active').index('div.item');
            $('ol.carousel-indicators li[data-slide-to="' + idx + '"]').addClass('active');
        });

        $('ol.carousel-indicators  li').on("click", function () {
            $('ol.carousel-indicators li.active').removeClass("active");
            $(this).addClass("active");
        });
    };
    this.GetNews = function (idlastnews, idothernews) {
        $('#newLastestloading').removeClass('hide');
        var currentId = 'newLastest';
        //get 3 latest thumbnails on homepage
        var itemrowlimit = 3;//getRowLimitFromContentEditorWP(currentId);
        var siteUrl = window.location.protocol + '//' + window.location.host + '/teams/OurVoiceHN/News/';
        var clientContext = new SP.ClientContext(siteUrl);
        var oList = clientContext.get_web().get_lists().getByTitle('Posts');
        var camlQuery = new SP.CamlQuery();

        camlQuery.set_viewXml('<View><Query><Where><Eq><FieldRef Name=\'Is_x0020_Active\' /><Value Type=\'Boolean\'>1</Value></Eq></Where><OrderBy><FieldRef Name=\'Top_x0020_News\' Ascending=\'FALSE\' /><FieldRef Name=\'PublishedDate\' Ascending=\'FALSE\' /></OrderBy></Query><RowLimit>' + itemrowlimit + '</RowLimit></View>');
        var items = oList.getItems(camlQuery);
        clientContext.load(items, "Include(ID,Title,Summary,Body,PublishingPageIcon,Top_x0020_News)");
        clientContext.executeQueryAsync(
            function () {
                var idlastnews1 = 0;
                var idlastnews2 = 0;
                var idlastnews3 = 0;
                $(idlastnews).html('');
                $(idothernews).html('');
                //set number of top news = 3
                var numberOfTopNews = 3;
                var markTopNews = 0;
                var listEnumerator = items.getEnumerator();
                var index = 0;

                while (listEnumerator.moveNext()) {
                    var item = listEnumerator.get_current();
                    var itemId = parseInt($.trim(item.get_item('ID'))) || 0;
                    var url = siteUrl + 'Lists/Posts/Post.aspx?ID=' + itemId;
                    markTopNews++;
                    if (markTopNews == 1) idlastnews1 = itemId;
                    if (markTopNews == 2) idlastnews2 = itemId;
                    if (markTopNews == 3) idlastnews3 = itemId;
                    //get 3 newest news on topnews
                    var news = '';
                    if (index == 0) {
                        news = '<div class="item active">';
                    } else {
                        news = '<div class="item">';
                    }

                    news += buildNewsLatest(item.get_item('PublishingPageIcon'), url, item.get_item('Title'), item.get_item('Summary')) + '</div>';
                    $(idlastnews).append(news);
                    index = index + 1;
                }

                //buid Indicators
                var indicators = '<ol class="carousel-indicators">';
                for (var i = 0; i < index; i++) {
                    if (i == 0) {
                        indicators += '<li data-target="#newlastestslide" data-slide-to="' + i + '" class="active"></li>';
                    }
                    else {
                        indicators += '<li data-target="#newlastestslide" data-slide-to="' + i + '" class=""></li>';
                    }
                }
                indicators += ' </ol>';
                $("#newlastestslide ol.carousel-indicators").each(function () { $(this).remove(); });
                if (index > 0) {
                    $("#newlastestslide").append(indicators);
                    activeIndicator();
                }
                $('#newLastestloading').addClass('hide');
                GetOtherNews(idlastnews, idothernews, idlastnews1, idlastnews2, idlastnews3);
            }, function (sender, args) {
                alert('Processing data. Please wait...');
            }
        );
    };
    this.GetOtherNews = function (idlastnews, idothernews, idlastnews1, idlastnews2, idlastnews3) {
        //get 3 latest thumbnails on homepage
        var itemrowlimit = 10;//getRowLimitFromContentEditorWP(currentId);
        var siteUrl = window.location.protocol + '//' + window.location.host + '/teams/OurVoiceHN/News/';
        var clientContext = new SP.ClientContext(siteUrl);
        var oList = clientContext.get_web().get_lists().getByTitle('Posts');
        var camlQuery = new SP.CamlQuery();

        //camlQuery.set_viewXml('<View><Query><Where><Eq><FieldRef Name=\'Is_x0020_Active\' /><Value Type=\'Boolean\'>1</Value></Eq></Where><OrderBy><FieldRef Name=\'Top_x0020_News\' Ascending=\'FALSE\' /><FieldRef Name=\'PublishedDate\' Ascending=\'FALSE\' /></OrderBy></Query><RowLimit>' + itemrowlimit + '</RowLimit></View>');
        camlQuery.set_viewXml('<View><Query><Where><And><And><And><Eq><FieldRef Name=\'Is_x0020_Active\' /><Value Type=\'Boolean\'>1</Value></Eq><Neq><FieldRef Name=\'ID\' /><Value Type=\'Counter\'>' + idlastnews1 + '</Value></Neq></And><Neq><FieldRef Name=\'ID\' /><Value Type=\'Counter\'>' + idlastnews2 + '</Value></Neq></And><Neq><FieldRef Name=\'ID\' /><Value Type=\'Counter\'>' + idlastnews3 + '</Value></Neq></And></Where><OrderBy><FieldRef Name=\'PublishedDate\' Ascending=\'FALSE\' /></OrderBy></Query><RowLimit>' + itemrowlimit + '</RowLimit></View>');
        var items = oList.getItems(camlQuery);
        clientContext.load(items, "Include(ID,Title,Summary,Body,PublishingPageIcon,Top_x0020_News)");
        clientContext.executeQueryAsync(
            function () {
                $(idothernews).html('');
                var listEnumerator = items.getEnumerator();
                while (listEnumerator.moveNext()) {
                    var item = listEnumerator.get_current();
                    var url = siteUrl + 'Lists/Posts/Post.aspx?ID=' + item.get_item('ID');
                    var othernews = buildOtherNews(item.get_item('PublishingPageIcon'), url, item.get_item('Title'), item.get_item('Summary'));
                    $(idothernews).append(othernews);
                }

            }, function (sender, args) {
                alert('Processing data. Please wait...');
            }
        );
    };
    this.buildNewsLatest = function (imageurl, itemurl, title, content) {
        if (title == null) { title = ""; }
        if (content == null) { content = ""; }

        if (content.length > 500) {
            content = content.substring(0, 500);
        }

        var imgUrl = '';
        if (imageurl != null && $(imageurl).attr('src') != "") {
            imgUrl = $(imageurl).attr('src') + '?RenditionID=6';
        }
        else {
            imgUrl = '/teams/OurVoiceHN/Style Library/Images/default_image.gif';
        }
        var result = '<a href="' + itemurl + '"><img class="img-responsive" src="' + imgUrl + '" /></a>';
        result += '<div class="newscontent caption">';
        result += '<h3><a href="' + itemurl + '">' + title + '</a></h3>';
        result += '<p class="content">' + content;
        result += '<span class="viewMore"><a href="' + itemurl + '">...More</a><span></p>';
        result += '</div>';
        return result;
    };

    this.buildOtherNews = function (imageurl, itemurl, title, content) {
        if (title == null) {
            title = "";
        }

        if (title.length > 50) {
            title = title.substring(0, 50) + '...';
        }

        if (content == null) {
            content = "";
        }

        if (content.length > 180) {
            content = content.substring(0, 180);
        }

        var imgUrl = '';
        if (imageurl != null && $(imageurl).attr('src') != "") {
            imgUrl = $(imageurl).attr('src');
        }
        else {
            imgUrl = '/teams/OurVoiceHN/Style Library/Images/default_image.gif';
        }

        var result = '<div class="newsitem col-sm-12 col-md-12 col-lg-12">';

        result += '<div class="row">';
        result += '<div class="newsleft col-sm-6 col-md-6 col-lg-6">';
        result += '<a href="' + itemurl + '"><img class="img-responsive" src="' + imgUrl + '" /></a>';
        result += '</div>';

        result += '<div class="newsright col-sm-6 col-md-6 col-lg-6">';

        result += '<h3 class="title"><a href="' + itemurl + '">' + title + '</a></h3>';
        result += '<p class="content">' + content + '</p>';
        result += '<a class="viewMore" href="' + itemurl + '">More</a></div>';
        result += '</div>';

        result += '</div>';

        result += '</div>';
        return result;
    };

}).call(this, jQuery);